package zsw.Serializable;

import org.springframework.core.serializer.Deserializer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.corba.se.pept.transport.EventHandler;

public class JSONSerializable {
	
	public static void main(String[] args) {
		JSONSerializable jsonSerializable=new JSONSerializable();
		
		Person person=new Person();
		person.setAge(12);
		person.setName("zws");
		person.setSex("男");
		String serialize = jsonSerializable.serialize(person);
		System.out.println(serialize);
		
		Person deserializer = jsonSerializable.deserializer(serialize, Person.class);
		System.out.println(deserializer);
	}
	
	public String serialize(Object object) {
		String writeValueAsString="";
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			 writeValueAsString = objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  writeValueAsString;
	}
	
	
	public  <T> T deserializer(String jsonStr,Class<T> clazz) {
		T readValue=null;
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			readValue = objectMapper.readValue(jsonStr, clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return readValue;
		
	}

}
